package 多线程.多线程创建.生产者消费者.同步代码块;

public class Product implements Runnable {

    private BaoZiPu baoZiPu;

    public Product(BaoZiPu baoZiPu) {
        this.baoZiPu = baoZiPu;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (baoZiPu) {
                if(baoZiPu.count == 50) {
                    return;
                }
                if (baoZiPu.isFlag()) {
                    // 有包子不用生产
                    try {
                        baoZiPu.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                baoZiPu.setCount();
                baoZiPu.setFlag(true);

                baoZiPu.notify();
            }
        }
    }
}
